/**
 * 项目费用维护
 * @author liuwei
 * @date 2019/12/05
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        table,
        vueData = {},
        table_head_where = {},
        table_where = {},
        cols = [[
            {type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: "GWGL001", width: 100, title: "招标编号", search: "default", searchOpts: {icon: true}}
            , {field: "GWGL002", width: 100, title: "项目名称", search: "default"}
            , {field: "GWGL003", width: 100, title: "开标日期", sort: true}
            , {field: "GWGL009", width: 100, title: "费用类型", filterDatas: app.datas_("gwgl009"),
                search: "default",searchOpts: {type:'select'}}
            , {field: "GWGL005_NAME", width: 100, title: "参标单位"}
            , {field: "GWGL006_NAME", width: 100, title: "付款单位"}
            , {field: "GWGL015", width: 100, title: "金额"}
            , {field: "GWGLORIU", width: 100, title: "申请人", hide: true, search: "default", searchOpts: {icon: true}}
            , {field: "GWGLORIU_NAME", width: 100, title: "申请人"}
            , {field: "GWGLORIG", width: 100, title: "申请部门", hide: true, search: "default", searchOpts: {icon: true}}
            , {field: "GWGLORIG_NAME", width: 100, title: "申请部门"}
            , {field: "GWGLACTI", width: 100, title: "状态", filterDatas: app.datas_("gwglacti"),
                search: "default",searchOpts: {type:'select'}}
            , {
                width: 60,
                title: '',
                toolbar: '#script-gwgi006setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwgi006SettingAction,
        moduleName = "投标报价单"
    ;

    var Gwai300 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwgi006',
                _searchs: [{
                    id: 'gwgi006', tableId: 'gwgi006_table', datas: _searchsArr
                }],
                _loadTplsFull: [
                    {
                        dom: 'gwgi006_edit', tpl: 'gwgi006_edit'
                    },
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    },
                    {
                        dom: 'checkApprovalProcess', tpl: 'checkApprovalProcess'
                    }
                ]
            }, win);
        },

        initHeadTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwgi006_table'),
                    url: g_ctxPath + 'crm/gwgi006/getHeadList',
                    exportsUrl: g_ctxPath + 'crm/gwgi006/getHeadList',
                    exportsName: '客户数据',
                    title: '项目费用申请表',
                    method: 'post',
                    id: 'gwgi006_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwgi006setting-action').innerHTML).render({}, function (string) {
                    gwgi006SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwgi006_table)', function (obj) {

                    layer.tips(gwgi006SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwgi006_edit',
                                            tagData: {
                                                obj: obj
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '项目费用申请编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                    case '2':
                                        self._openTplFull({
                                            tag: 'gwgi006_edit',
                                            tagData: {
                                                obj: obj
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '项目费用申请编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行单击事件
                table.on('row(gwgi006_table)', function (obj) {
                    table_where.GWGA001 = obj.data.GWGA001;
                    obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                });

                //监听行双击事件
                table.on('rowDouble(gwgi006_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwgi006_edit',
                        tagData: {
                            obj: obj,
                            detailReadOnly: true
                        },
                        index: 0,
                        openOpts: {
                            title: '项目费用申请编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwgi006_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwgi006_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: '项目费用申请编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.delDataBatch();
                                }
                            });
                            break;
                        case 'submit' :
                            self.changeStatusBatch("submit");
                            break;
                        case 'cancelSubmit' :
                            self.changeStatusBatch("cancelSubmit");
                            break;
                        case 'checkProcess' : // 查看审批流程
                            self.checkProcess();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwgi006_table', where: table_head_where});
        },

        changeStatusBatch: function (action) {
            var checkStatus = table.checkStatus('gwgi006_table');
            var flag = true;
            if (checkStatus.data.length === 0) {
                app.showWarning("至少选择一笔数据进行操作");
            }
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    let _temp = app.fields_('GWGL_T');
                    switch (action) {
                        case "submit" :  // 提交
                            if (v.GWGLACTI === "N" || v.GWGLACTI === "R") {
                                v.GWGLACTI = "T"
                            } else {
                                app.showWarning("未审核或拒审的资料才能提交");
                                flag = false;
                                return;
                            }
                            break;
                        case "cancelSubmit" : // 取消提交
                            if (v.GWGLACTI === "T") {
                                v.GWGLACTI = "N"
                            } else {
                                app.showWarning("已经提交的资料才能取消提交");
                                flag = false;
                                return;
                            }
                            break;
                    }
                    _xdata.push({
                        GWGLID: v.GWGLID,
                        GWGLENT: v.GWGLENT,
                        GWGLACTI: v.GWGLACTI,
                        GWGL024: v.GWGL024
                    });
                });
                if (!flag) {
                    return;
                } else {
                    app.ajax({
                        xurl: 'crm/gwgi006/changeStatus',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: moduleName + '状态修改成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        checkProcess:function() {
            var checkStatus = table.checkStatus('gwgi006_table');
            if(checkStatus.data.length === 0) {
                app.showWarning("请选择一笔数据进行操作");
                return;
            }
            if(checkStatus.data.length > 1) {
                app.showWarning("只能选择一笔数据进行操作");
                return;
            }
            let data = checkStatus.data[0];
            if(data.GWGLACTI != 'T') {
                if(checkStatus.data.length > 1) {
                    app.showWarning("已提交的资料才能查看审批流程");
                    return;
                }
            }

            let fd_id = isTest?"16ebfcb252b9da37c0cabe74ae3804a1":"16f022010891abf64e950b44ea4925ba";
            self._openTplFull({
                tag: 'checkApprovalProcess',
                tagData: {
                    obj: {
                        fd_test_id:"16ebfcb252b9da37c0cabe74ae3804a1",
                        fd_formal_id:"16f022010891abf64e950b44ea4925ba",
                        fd_process_id:data.GWGL024
                    }
                },
                index: 2,
                openOpts: {
                    title: "审批流程",
                    area: ['800px','400px']
                }
            });
        },

        delData: function (obj) {
            if (obj.data.GWGLACTI !== 'N' && obj.data.GWGLACTI !== 'R') {
                app.showWarning("只有未审核或拒审的资料才能删除");
                return;
            }
            let param = {
                GWGLENT: obj.data.GWGLENT,
                GWGLID: obj.GWGLID
            };
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwgi006/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '项目费用申请删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function () {
            let checkStatus = table.checkStatus('gwgi006_table');
            let canDelete = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if (v.GWGLACTI !== 'N') {
                        canDelete = false;
                        return;
                    }
                    var _temp = app.fields_('crm_gwgi006');
                    _temp.GWGLID = v.GWGLID;
                    _temp.GWGLENT = v.GWGLENT;
                    _xdata.push(_temp);
                });
                if (!canDelete) {
                    app.showWarning("只有未审核或拒审的资料才能删除")
                } else {
                    app.ajax({
                        xurl: 'crm/gwgi006/delBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: '项目费用申请删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: vueData,
                    searchId: 'gwgi006',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field === 'GWGL001') {  // 招标编号
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwgc001',
                                            columnList: [
                                                {label: '招标编号', prop: 'GWGC001', width: '150'},
                                                {label: '项目名称', prop: 'GWGC022', width: '150'},
                                                {label: '开标时间', prop: 'GWGC024', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWGL001 = data.GWGC001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择招标编号',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field === 'GWGLORIG') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysorg_id',
                                            columnList: [
                                                {label: '组织编号', prop: 'ID', width: '150'},
                                                {label: '组织名称', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWGLORIG = data.ID;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择所属组织',
                                        area: ['600px', '430px']
                                    }
                                });
                            } else if (field === 'GWGLORIU') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysuser_id',
                                            columnList: [
                                                {label: '人员编号', prop: 'ID', width: '150'},
                                                {label: '姓名', prop: 'NAME', width: '150'},
                                                {label: '账户', prop: 'ACCOUNT', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWGLORIU = data.ID;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择申请人',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initHeadTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwai300();

});